/**
 * 全局 dialog 弹窗
 * @author scutterry
 * @create 2018-04-01
 * 
 * todo 需要处理确定和删除按钮
 */
let template = require('./panel.html');
let WLLib;
if (window && window.WLLib) {
  WLLib = window.WLLib;
} else {
  WLLib = {};
  if (window) {
    window.WLLib = WLLib;
  }
}
var Vue;
var factory;

/**
 * 初始化 vue
 * @param {Vue} vue 用来创建新的Vue 组件 
 */
WLLib.init = function (vue, func) {
  Vue = vue;
  factory = func;
}

function createDiv(id) {
  var m = document.getElementById(id);
  if (m == null) {
    m = document.createElement('div');
    m.id = id;
    document.getElementsByTagName('body')[0].appendChild(m);
  } else {
  }
  return m;
}

var vueDic = {};
/**
 * 
 * 该WLLib对象放置所有的共用方法
 * WLLib.panel()是调用弹框的方法
 */
WLLib.panel = function (comp, option) {
  var v = vueDic[comp];
  var m = createDiv(comp);
  if (!option) {
    option = {};
    option.title = '';
    option.style = '';
    option.panel_class = '';
  }
  var _d = WLLib.clone(option.data == null ? {} : option.data);
  if (!v) {
    var com = factory(comp);
    v = new Vue({
      name: 'popup_' + comp,
      el: m,
      data: function () {
        return {
          title: option.title,
          style: option.style,
          //   size: option.size == null ? 'mini' : option.size,
          //   size: 'small',
          modal: true,
          panel_class: option.panel_class,
          close_on_press_escape: false,
          show_close: true,
          show: true,
          data: _d,
        };
      },
      template: template,
      mounted: function () { },
      methods: {
        handleClose(done) {
          if (option.close) {
            option.close();
          }
          done();
        },
        close() {
          if (option.close) {
            option.close();
          }
          this.show = false;
        },
        callback(result) {
          console.log("callba");
          if (option._source != null) {
            option.callback.call(option._source, result);
            this.show = false;
            return;
          }
          //如果不传type或者type等于close或cancel直接关闭弹框
          if (!result.type || result.type == 'close' || result.type == 'cancel') {
            this.show = false;
          } else if (result.type == 'sure') {
            //如果type等于sure则调用parent传递过来的回调函
            this.show = false;
            if (option.callback) {
              option.callback(result.data);
            }
          }
        },
      },
      components: {
        child: com,
      },

    });
    vueDic[comp] = v;
  } else {
    v.show = true;
    // v.data.msg = "random " + Math.random();
    v.data.panel_class = option.panel_class;
  }
  return v;
};

WLLib.clone = function clone(obj) {
  // Handle the 3 simple types, and null or undefined
  if (null == obj || 'object' != typeof obj) return obj;

  // Handle Date
  if (obj instanceof Date) {
    var copy = new Date();
    copy.setTime(obj.getTime());
    return copy;
  }

  // Handle Array
  if (obj instanceof Array) {
    var copy = [];
    for (var i = 0, len = obj.length; i < len; ++i) {
      copy[i] = clone(obj[i]);
    }

    return copy;
  }

  // Handle Object
  if (obj instanceof Object) {
    var copy = {};
    for (var attr in obj) {
      if (obj.hasOwnProperty(attr)) copy[attr] = clone(obj[attr]);
    }

    return copy;
  }

  throw new Error("Unable to copy obj! Its type isn't supported.");
};

var dialogTemp = require('./dialog.html');
var modalStack = [];
var modalId = 0;
WLLib.dialog = function (title, content, confirm, ths) {
  var v = modalStack.pop();
  if(v){
    console.log("from modal stack");
    v.title = title;
    v.content = content;
    v.confirmCallback = confirm;
    v.confirmThis = ths;
    v.visible = true;
    return;
  }
  modalId++;
  var m = createDiv('wlDialog' + modalId);
  v = new Vue({
    el: m,
    template: dialogTemp,
    props:{
      closeFlag:{
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        title: title,
        content: content,
        confirmCallback: confirm,
        confirmThis: ths,
        visible: true,
        cancelLabel: "取 消",
        confirmLabel: "确 定",
      }
    },

    methods: {
      confirm() {
        console.log("popup confirm");
        if (typeof this.confirmCallback === 'function') {
          this.confirmCallback.call(this.confirmThis);
        }else{
          this.close();
        }
      },
      handleClose(done) {
        done();
      },
      close() {
        this.visible = false;
        modalStack.push(this);
      },
    }
  });
}


export default WLLib;
